package zuochengyun.node;

import zuochengyun.class04.Code03_DoubleLinkedListToDeque;

public class DoubleLinkedListToDeque<V> {
    public static class DoubleNode<V>{
        public V value;
        public DoubleNode<V> last;
        public DoubleNode<V> next;

        public DoubleNode(V data){
            last = null;
            next = null;
            this.value = data;
        }
    }
    public static class MyDeque<V>{

        public int size ;
        public DoubleNode<V> head;
        public DoubleNode<V> tail;

        public MyDeque(){
            size = 0;
            head = null;
            tail = null;
        }

        public boolean isEmpty(){
            return size==0;
        }

        public int size(){
            return size;
        }

        public void pushHead(V value){
            DoubleNode cur = new DoubleNode(value);
            if(head==null){
                head = cur;
                tail = cur;
            }else{
                cur.next = head;
                head.last = cur;
                head = cur;
            }
            size++;
        }

        public void pushTail(V value){
            DoubleNode cur = new DoubleNode(value);
            if(head==null){
                head = cur;
                tail = cur;
            }else{
                tail.next = cur;
                cur.last = tail;
                tail = cur;
            }

            size++;
        }

        public V popHead(){
            if(head==null){
                return null;
            }
            size--;
            V ans = head.value;
            if(head==tail){
                head = null;
                tail = null;
            }else{
                head = head.next;
                head.last = null;
            }
            return ans;
        }

        public V popTail(){
            if(tail==null){
                return null;
            }
            size--;
            V ans = tail.value;
            if(head==tail){
                head=null;
                tail=null;
            }else{
                tail = tail.last;
                tail.next = null;
            }
            return ans;
        }
    }
}

